package cn.youyeku.zsxh.ui.teacher;

import android.graphics.Color;
import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.ConverUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.bean.Statistics;
import cn.youyeku.zsxh.bean.question_data;
import cn.youyeku.zsxh.bean.Question;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.BaseActivity;
import lecho.lib.hellocharts.formatter.SimpleAxisValueFormatter;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;


/**
 * Created by youye on 17-4-17.
 */

public class GraphicQuestion extends BaseActivity {
    private ColumnChartView colum_chart;//图表view
    private ColumnChartData colum_data;//图表数据Model
    private List<Question> questions = new ArrayList<>();
    private List<Statistics> statistics_data = new ArrayList<>();
    private List<SubcolumnValue> values;
    private  int obj_len;

//    private List<AxisValue> Axis_Y = new ArrayList<>();

    private int QuestionStudent_total;
//        private int[] arr_len = new int[]{1, 5, 9, 14, 8, 25, 31, 50, 40, 20, 3, 0, 65, 65, 50, 74, 20, 83, 90, 65, 65};
    private int[] arr;
    private Axis axisX;
    private Axis axisY;

    private List<Column> xcolumValues = new ArrayList<>();

    ArrayList<AxisValue> axisValuesX = new ArrayList<>();
    ArrayList<AxisValue> axisValuesY = new ArrayList<>();
    private boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_graphic);
        initView();


        question_data question_data = (question_data) getIntent().getSerializableExtra("data");
        setTitle(question_data.getQuestion_desc());
        Gson gson = new Gson();

        //获取问题的数据
        questions = gson.fromJson(ConverUtils.Base64ToString(question_data.getQuestion_data()), new TypeToken<List<Question>>() {
        }.getType());
        LogUtils.w(question_data.getQuestion_sum());
    if (!question_data.getQuestion_sum().equals("")){

        statistics_data = gson.fromJson(question_data.getQuestion_sum(), new TypeToken<List<Statistics>>() {
        }.getType());

        initStatistics();

        LogUtils.w(question_data.getQuestion_sum());

//        arr = gson.fromJson(question_data.getQuestion_sum(),int[]);
        initColumData();

    }else {
        app.backgroundThreadShortToast("还没有人做题目");
    }

    }

    private void initStatistics() {
        //单个数据统计List<Integer> all_data;

        List<Integer> all_data;
        QuestionStudent_total = statistics_data.size();
         obj_len = statistics_data.get(0).getQue_count().size();
        arr = new int[obj_len];
        for (Statistics tmp :
                statistics_data) {
            all_data = tmp.getQue_count();
            for (int j = 0; j < obj_len; j++) {
                arr[j] += all_data.get(j);
            }

        }
//        //取反数组
//        for (int i = 0; i< obj_len; i++){
//            arr[i] = obj_len - arr[i];
//        }
    }


    private void initColumData() {

        //设置X  Y的刻度

        for (int i = 0; i <obj_len+10; i+=3) {
            axisValuesY.add(new AxisValue(i).setLabel(i+"人"));
        }

        for(int i=0;i<obj_len+1;i++){
            if(i==0){
                axisValuesX.add(new AxisValue(i).setLabel("总人数"));
            }else {
                axisValuesX.add(new AxisValue(i).setLabel("第"+i+"题"));
            }
        }


        add_data();

        X_Y_SET();

        colum_data = new ColumnChartData(xcolumValues);

        //堆叠数据
        colum_data.setStacked(false);
        colum_data.setAxisXBottom(axisX);
        colum_data.setAxisYLeft(axisY);



          initData();

//开始动画形式开始 不要用这东西会让你的设置的值失效
//        colum_chart.startDataAnimation();

        resetView();


    }

    private void initData() {
        //设置数据
        colum_chart.setColumnChartData(colum_data);



        //数据间隔 2px  间隔样式有关数据都在数据上
        colum_data.setFillRatio(0.5f);

        //进行缩放
        colum_chart.setZoomEnabled(true);
        //设置缩放模式，水平（只对x轴缩放）
        colum_chart.setZoomType(ZoomType.HORIZONTAL);

        //设置缩放级别，1为默认，最大级别是20；
        colum_chart.setZoomLevel(0, 0, 1);
        //于是我这样用了

        //允许滚动
        colum_chart.setScrollEnabled(true);


        //设置是否能点击值
        colum_chart.setValueTouchEnabled(true);

        colum_chart.setOnValueTouchListener(new ColumnChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int columnIndex, int subcolumnIndex, SubcolumnValue value) {

                if (columnIndex != 0) {
                    UIControl.showGraphicQuestionChat(getApplicationContext(), questions.get(columnIndex - 1));
                }

//                app.backgroundThreadShortToast(arr[columnIndex - 1] + " " + columnIndex + "sub " + subcolumnIndex);

            }

            @Override
            public void onValueDeselected() {

            }
        });
    }

    private void X_Y_SET() {
        axisX = new Axis();
        axisY = new Axis().setHasLines(true);
        axisX.setName("题目 Y轴显示的是做错的人数");
        axisY.setName("做错的人数 Y轴");

        //坐标刻度
        axisX.setMaxLabelChars(5);
//        axisY.setMaxLabelChars(2);
//        axisX.setAutoGenerated(true);
//        axisY.setAutoGenerated(true);
        axisX.setTextColor(Color.parseColor("#B2000000"));
        axisY.setTextColor(Color.parseColor("#B2000000"));


        //倾斜体数据
//        axisX.setHasTiltedLabels(true);
        //显示轴变得分割线
        axisX.setHasSeparationLine(true);


        //设置精度，缩放时坐标轴精度默认会增加，而int不可能出现小数
        axisX.setFormatter(new SimpleAxisValueFormatter(0));
        axisY.setFormatter(new SimpleAxisValueFormatter(0));

        axisX.setValues(axisValuesX);

        axisY.setValues(axisValuesY);


    }

    private void add_data() {
        //添加0位置的初始值
        addColumnValue(QuestionStudent_total, 0);



//        //循环设置变量
//        for (int i = 0; i < obj_len; i++) {
//            addColumnValue(arr[i], i + 1);
//        }
        for (int i = 0; i < obj_len; i++) {
            addColumnValue(arr[i], i + 1);
        }

    }


    private void addColumnValue(int data, int pos) {


        int color = Color.parseColor("#fffeca86");

        switch (data / 10) {
            case 1:
                color = ChartUtils.COLOR_RED;
                break;
            case 3:
                color = ChartUtils.COLOR_VIOLET;
                break;
            case 5:
                color = Color.parseColor("#FFCC66B9");
                break;
            case 6:
                color = Color.parseColor("#FFEFE15E");
                break;
            case 7:
                color = Color.parseColor("#FF80BE7A");
                break;
            case 9:
                color = Color.parseColor("#FF4DE93E");
                break;
            case 10:
                color = Color.parseColor("#FFE49618");
                break;

        }
        if (isFirst) {
            color = Color.parseColor("#3681de");
            isFirst = false;
        }
        Column colum;
        values = new ArrayList<>();
//        data+=25;

        values.add(new SubcolumnValue(data, color));
        colum = new Column(values);
        colum.setHasLabels(true);
        xcolumValues.add(colum);




    }



    private void initView() {
        colum_chart = (ColumnChartView) findViewById(R.id.chart);


    }

    private void resetView() {
        Viewport viewport= new Viewport(colum_chart.getMaximumViewport());
        viewport.top=+QuestionStudent_total+10;
        viewport.right=obj_len+1;

        colum_chart.setCurrentViewportWithAnimation(viewport);
        colum_chart.setMaximumViewport(viewport);
        colum_chart.setCurrentViewport(viewport);
    }
}
